package com.example.chapter07;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;

public class CircleServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置响应内容类型
        response.setContentType("text/html;charset=UTF-8");

        // 获取表单提交的半径值
        String radiusStr = request.getParameter("radius");
        Circle circle = new Circle();
        Double radius = Double.parseDouble(radiusStr);
        circle.setRadius(radius);

        // 计算圆的面积
        double area = circle.calculateArea();

        // 获取PrintWriter对象用于向客户端输出数据
        PrintWriter out = response.getWriter();
        out.println("<html><body>");
        out.println("<h2>圆面积是: " + area + "</h2>");
        out.println("</body></html>");
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}